//
//  XMBlackListViewController.m
//  XunMi
//
//  Created by MacBook on 2024/2/29.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMBlackListViewController.h"
#import "XMUserListTableViewCell.h"
#import "XMUserDetailViewController.h"
#import "XMConversationViewController.h"
@interface XMBlackListViewController ()
@property (nonatomic, strong) NSMutableArray <XMUserModel *> *dataArray;
@property (nonatomic, assign) NSInteger page;
@end

@implementation XMBlackListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initVariables];
    [self setupSubviews];
    [self blackListData];
    [self setupRefresh];
}

#pragma mark - 初始化变量
- (void)initVariables {
    self.dataArray = [NSMutableArray array];
    self.page = 1;
}

#pragma mark - refresh
- (void)setupRefresh {
    XMWeakSelf(self);
    self.tableView.mj_header = [XMRefreshNormalHeader headerWithRefreshingBlock:^{
        XMStrongSelf(self);
        [self blackListDataRefresh];
    }];
    
    XMRefreshBackFooter *footer = [XMRefreshBackFooter footerWithRefreshingBlock:^{
        XMStrongSelf(self);
        self.page += 1;
        [self blackListData];
    }];
    self.tableView.mj_footer = footer;
    self.tableView.mj_footer.hidden = YES;
}

- (void)blackListDataRefresh {
    self.page = 1;
    [self.dataArray removeAllObjects];
    [self blackListData];
}

- (void)blackListData {
    [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
    NSMutableDictionary *dic = @{
        @"page_no": @(self.page),
        @"page_size": @(10),
    }.mutableCopy;
    
    [YHNetworkManager.sharedInstance postWithUrl:@"api/index/blocks" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        [self.tableView hideStatusView];
        if (tip.success) {
            [MBProgressHUD hideHUD];
            NSArray *record = [result safe_objectForKey:@"record"];
            NSArray *array = [XMUserModel mj_objectArrayWithKeyValuesArray:record];
            [self.dataArray addObjectsFromArray:array];
            if (self.dataArray.count == 0) {
                [self.tableView showStatusView:(YHViewStatusTypeEmptyData)];
                self.tableView.statusTitleLabel.text = @"黑名单列表为空哦~";
            }
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        self.tableView.mj_footer.hidden = self.dataArray.count == 0;
        if (tip.loadMore) {
            [self.tableView.mj_footer endRefreshing];
        } else {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}

#pragma mark - setupSubviews
- (void)setupSubviews {
    [self.navLeft2Btn setTitle:@"黑名单" forState:UIControlStateNormal];
    [self tableViewRegisterNibWithNibName:kStringFromClass(XMUserListTableViewCell)];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kUserListCellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMUserListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kStringFromClass(XMUserListTableViewCell)];
    [cell setupCellWithUser:self.dataArray[indexPath.row] type:(XMUserListCellBlack)];
    XMWeakSelf(self);
    cell.actBlock = ^(XMUserModel * _Nonnull model, XMUserListCellType type) {
        XMStrongSelf(self);
        [MBProgressHUD showHUDActivityMsg:nil  toView:kWindow];
        NSMutableDictionary *dic = @{
            @"target_user_id": model.user_id
        }.mutableCopy;
        [YHNetworkManager.sharedInstance postWithUrl:@"api/chat/blacklist/edit" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
            if (tip.success) {
                [MBProgressHUD hideHUD];
                [MBProgressHUD showHUDSucc:@"取消拉黑成功" toView:kWindow];
                [self.dataArray removeObjectAtIndex:indexPath.row];
                [self.tableView reloadData];
                if (self.dataArray.count == 0) {
                    [self.tableView showStatusView:(YHViewStatusTypeEmptyData)];
                    self.tableView.statusTitleLabel.text = @"黑名单列表为空哦~";
                }
            }
        }];
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    XMUserModel *model = self.dataArray[indexPath.row];
    XMUserDetailViewController *vc = [[XMUserDetailViewController alloc] init];
    vc.userId = model.user_id;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
